﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="addContent.aspx.cs" Inherits="background.Manager.Content.addContent" %>

<!DOCTYPE html>
<% 
    if(string.IsNullOrEmpty(Request["actionType"])) { Response.Write("缺少actionType参数"); return; }
    if(!new Regex("^(add)$").IsMatch(Request["actionType"])) { Response.Write("actionType参数：只能是add"); return; }
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="../Styles/index.css" rel="stylesheet" />
    <script src="../Scripts/jquery-1.8.2.min.js"></script>
    <script src="../Scripts/layer/layer.js"></script>
    <script src="../Scripts/ueditor_1.4.3.1_utf8/ueditor.config.js"></script>
    <script src="../Scripts/ueditor_1.4.3.1_utf8/ueditor.all.min.js"></script>
    <script type="text/javascript">
        function changeType() {
            var t = $("#group option:selected").attr("data-type");
            if (t == "cover") {
                $(".cover").show();
            } else {
                $(".cover").hide();
            }
        }
    </script>
</head>
<body>
    <div class="top">
        <strong>编辑内容</strong>
        <span style="float: right"><a href="javascript:history.back();">
            <img src="../Images/bak.png" />后退</a></span>
    </div>
    <div class="main">
        <!--表单提交-->
        <div>
            <table border="0" cellpadding="0" cellspacing="0" class="table">
                <tr>
                    <th>所属分组</th>
                    <td>
                        <% System.Data.DataTable group=Codes.ContentGroup.getList(1, 9999); %>
                        <select id="group" class="text" onchange="changeType()">
                            <option value="" data-type="">--请选择--</option>
                            <% foreach(System.Data.DataRow dr in group.Rows) {%>
                            <option value="<%=dr["id"] %>" <%=dr["id"].ToString()==Request["id"].ToString()?"selected='selected'":"" %> data-type="<%=dr["contentType"] %>"><%=dr["title"] %><%=dr["state"].ToString()=="0"?"(禁用)":"" %></option>
                            <%} %>
                        </select><i>*</i>&nbsp;<a href="ContentGroup.aspx">管理分组</a>
                        </td>
                </tr>
                <tr>
                    <th>标题</th>
                    <td>
                        <input type="text" id="title" class="text" value="" maxlength="255" /><i>*</i></td>
                </tr>
                <tr>
                    <th>作者</th>
                    <td>
                        <input type="text" id="author" class="text" value="" maxlength="255" /></td>
                </tr>
                <tr class="cover">
                    <th>封面</th>
                    <td>
                        <input type="text" id="cover" class="text" value="" placeholder="点击上传按钮上传封面图片" />&nbsp;<input type="button" class="sbtn" onclick="upImage();" value="上传" /></td>
                </tr>
                <tr>
                    <th>摘要</th>
                    <td>
                        <textarea id="summary" class="text" maxlength="255" style="height: 90px; width: 277px"></textarea><i>最多输入255个字符</i>
                    </td>
                </tr>
                <tr>
                    <th style="vertical-align: top">内容</th>
                    <td style="height: 50px; line-height: 20px;">
                        <script type="text/plain" id='myEditor' style="width: 900px; height: 400px;"></script>
                        <script type="text/javascript">
                            var ue = UE.getEditor('myEditor');
                        </script>
                    </td>
                </tr>
                <tr>
                    <th>排序</th>
                    <td>
                        <input type="text" id="order" class="text" value="0" /><i>数字越小越靠前，此排序影响后台配置前后显示顺序</i></td>
                </tr>
                <tr>
                    <th>标签</th>
                    <td>
                        <input type="text" id="label" class="text" value="" /><i>多个标签请用“空格”隔开</i>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" class="sbtn" value="保存" onclick="submit_save()" /></td>
                </tr>
            </table>
        </div>
    </div>
    <script type="text/plain" id="upload_ue"></script>
    <script type="text/javascript">
        /*----------------------上传图片-------------------------*/
        var o_ueditorupload = UE.getEditor('upload_ue', { toolbars: [["insertimage"]], autoHeightEnabled: false });
        o_ueditorupload.ready(function () {
            o_ueditorupload.hide();
            o_ueditorupload.addListener('beforeInsertImage', function (t, arg) {
                $("#cover").val(arg[0].src);
            });
        });
        function esc() { $("#cover").val(""); }
        function upImage() { o_ueditorupload.getDialog("insertimage").open(); }
        /*----------------------上传图片-------------------------*/

        function submit_save() {
            var _group = $("#group option:selected").val(),
                _isCover = $("#group option:selected").attr("data-type"),
                _title = $("#title"),
                _author = $("#author"),
                _cover = $("#cover"),
                _summary = $("#summary"),
                _content = ue.getContent(),
                _order = $("#order"),
                _label = $("#label");
            if (_group.length == 0) { layer.msg("请选择所属分组"); return; }
            if ($.trim(_title.val()).length == 0) { layer.msg("请输入文章标题"); _title.focus(); return; }
            if (_isCover.toString() == "cover") {
                if ($.trim(_cover.val()).length == 0) {
                    layer.msg("请上传封面图片"); _cover.focus(); return;
                }
            }
            if (!ue.hasContents()) { layer.msg("请输入文章内容"); ue.focus(); return; }
            if ($.trim(_order.val()).length > 0) {
                if (!/^\d+$/.test(_order.val())) {
                    layer.msg("请正确输入排序"); _order.focus(); return;
                }
            }
            var data = {
                action: "add",
                group: _group,
                title: _title.val(),
                author: _author.val(),
                cover: _cover.val(),
                summary: _summary.val(),
                content: _content,
                order: _order.val(),
                label: _label.val()
            }
            $.post("Action/content.ashx", data, function (ret) {
                if (ret) {
                    layer.msg("恭喜您保存成功", { icon: 1, time: 2000 }, function () {
                        window.location.href = "ContentList.aspx"
                    })
                } else {
                    layer.msg("对不起保存失败", { icon: 2, time: 2000 })
                }
            })
        }
    </script>
    <!--#include file='../Include/footer.html'-->
</body>
</html>
